var verifyStatus = 9;
$(function () {
    if($.util.getRequest('dictKey') != 'activity'){
        $(".activity").css('display','none');
    }
    initDatagrid();
    $("#verifyStatus").combobox({
        onSelect:function(res){
            status = res.value;
            verifyStatus = status;
        }
    });
    $("#queryButton").click(function () {

        var beginTime = $('#beginTime').datetimebox('getValue');
        var endTime = $('#endTime').datetimebox('getValue');
        if(beginTime != "" && endTime == ""){$.messager.alert('提示','未选择结束时间','info');return;}
        if(beginTime == "" && endTime != ""){$.messager.alert('提示','未选择开始时间','info');return;}
        if(beginTime>endTime){
            $.easyui.messager.alert("开始时间不能大于结束时间");
            return;
        }
        if(beginTime != "" && endTime != ""){
            $("#listTable").datagrid('load',{
                groupType: $.util.getRequest('dictKey'),
                queryStr: $("#queryStr").val(),
                beginTime: $("input[name=beginTime]").val(),
                endTime: $("input[name=endTime]").val(),
                verifyStatus:verifyStatus
            })
        }else{
            $("#listTable").datagrid('load',{
                groupType: $.util.getRequest('dictKey'),
                queryStr: $("#queryStr").val(),
                verifyStatus:verifyStatus
            })
        }
    });
});

// 表格内容初始化
function initDatagrid() {
    var url = $.util.getRequestJson('/json/sysMessage.listByGroup.json');
    var columns;
    if($.util.getRequest('dictKey') == 'activity'){
        columns = [
            [
                {
                    field: 'informTitle',
                    title: '标题'
                },
                {
                    field: 'createPerson',
                    title: '发布人',
                    align: 'center',
                    width: 10,
                    formatter: function (value, row) {
                        return value == null ? "——" : value;
                    }
                },
                {
                    field: 'createTime',
                    title: '发起时间',
                    align: 'center',
                    width: 15
                },
                {
                    field: 'haveRead',
                    title: '已阅读',
                    align: 'center',
                    width: 10
                },
                {
                    field: 'beginTime',
                    title: '生效时间',
                    align: 'center',
                    width: 15
                },
                {
                    field: 'endTime',
                    title: '失效时间',
                    align: 'center',
                    width: 15
                },
                {
                    field:'verifyStatus',
                    title:'审核状态',
                    align: 'center',
                    width:10,
                    formatter:function (value, row, index) {
                        if(value == 0){return '<font color=\'red\'>未通过</font>'};
                        if(value == 1){return '<font color=\'green\'>已通过</font>'};
                        if(value == null || value == 3){return '待审核'};
                    }
                },
                /*{
                    field: 'noRead',
                    title: '未阅读',
                    width: 15
                },
                {
                    field: 'readRate',
                    title: '阅读率',
                    width: 15,
                    formatter: function (value, row) {
                        return row.haveRead / (row.haveRead + row.noRead) * 100 + "%";
                    }
                },*/
                {
                    field: 'context',
                    width: 20,
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row) {
                        var aEl = '<a href=javascript:findDetail("{0}"); class="easyui-tooltip" title="查看">查看</a>' +
                            ' | <a href=javascript:editNotice("{0}"); class="easyui-tooltip" title="删除">编辑</a>' +
                            ' | <a href=javascript:deleteNotice("{0}"); class="easyui-tooltip" title="删除">删除</a>';
                        //格式化操作按钮
                        return $.string.format(aEl, row.id);
                    }
                }
            ]
        ];
    }else{
        columns = [
            [
                {
                    field: 'informTitle',
                    title: '标题',
                    width: 20
                },
                {
                    field: 'createPerson',
                    title: '发布人',
                    align: 'center',
                    width: 10,
                    formatter: function (value, row) {
                        return value == null ? "——" : value;
                    }
                },
                {
                    field: 'createTime',
                    title: '发起时间',
                    align: 'center',
                    width: 15
                },
                {
                    field: 'haveRead',
                    title: '已阅读',
                    align: 'center',
                    width: 10
                },
                /*{
                    field: 'noRead',
                    title: '未阅读',
                    width: 15
                },
                {
                    field: 'readRate',
                    title: '阅读率',
                    width: 15,
                    formatter: function (value, row) {
                        return row.haveRead / (row.haveRead + row.noRead) * 100 + "%";
                    }
                },*/
                {
                    field: 'context',
                    width: 20,
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row) {
                        var aEl = '<a href=javascript:findDetail("{0}"); class="easyui-tooltip" title="查看">查看</a>' +
                            ' | <a href=javascript:editNotice("{0}"); class="easyui-tooltip" title="删除">编辑</a>' +
                            ' | <a href=javascript:deleteNotice("{0}"); class="easyui-tooltip" title="删除">删除</a>';
                        //格式化操作按钮
                        return $.string.format(aEl, row.id);
                    }
                }
            ]
        ];
    }
    $('#listTable').datagrid({
        // type=0 即查询 普通公告类型数据
        url: url /*+ '?type=0'*/,
        idField: 'id',
        queryParams: {
            groupType: $.util.getRequest('dictKey')
        },
        fit: true,
        pagination: true,
        pageSize: 30,
        checkOnSelect: false,
        selectOnCheck: false,
        singleSelect: true,
        toolbar: '#toolbarButton',
        fitColumns: true,
        columns: columns
    });
}

// 添加新公告或积分规则
function editNotice(id) {
    var title = id ? "编辑" : "新增";
    var url = $.util.getRootPath() + '/page/operatorManager/notice/publishNotice.html';
    url = id ? url + '?id=' + id : url;
    var dialog = $.easyui.showDialog({
        iconCls: "",
        title: title,
        width: 800,
        height: 500,
        maximizable: true,
        href: url,
        topMost: true,
        saveButtonText: '保存',
        onSave: function () {
            subForm(dialog);
        }
    });
}

// 查看详情
function findDetail(id) {
    var title = "查看";
    var url = $.util.getRootPath() + '/page/operatorManager/notice/noticeDetail.html?id=' + id;
    //url = id ? url + '?id=2&typeId=' + typeId : url;
    var dialog = $.easyui.showDialog({
        iconCls: "",
        title: title,
        width: 600,
        height: 400,
        maximizable: true,
        href: url,
        topMost: true,
        saveButtonText: '发布',
        enableSaveButton:false
        // onSave: function () {
        //     subForm(dialog);
        // }
    });

}

// 删除公告内容
function deleteNotice(id) {
    $.messager.confirm('确认对话框', '您确定要删除该条公告信息吗？', function (r) {
        if (r) {
            var deleteUrl = "/json/operator.notice.deleteById.json";
            var url = $.util.getRequestJson('/json/sysMessage.updateStatus.json');
            $.util.get(url, {id: id,status:-1}, function (res) {
                // 删除完成重新加载
                if (res.success) {
                    $('#listTable').datagrid('reload');
                }
            });
        }
    });
}

// 保存项目内容
function subForm(dialog) {
    var messageType = parent.$("#messageType").combobox('getValue');
    if (parent.$("#context").ueditor('getPlainTxt') == "") {
        parent.$.messager.alert('请先填写发布信息的内容');
        return;
    }
    var url = $.util.getRootPath() + '/sysMessage/save';
    var form = parent.$('#publishNotice').form('submit', {
        url: url,
        onSubmit: function (param) {
            return parent.$('#publishNotice').form('validate');
        },
        success: function (data) {
            if (data.success) {
                $('#listTable').datagrid('reload');
                dialog.dialog('close');
            }
            $.easyui.messager.show(data.message);
        }
    });
}

